﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Logic.Interface;
using System.Collections.Generic;

public partial class Education_Report_ScorePointEduReport : System.Web.UI.Page
{
    public List<ScorePointEducation> listScoreSettings = new List<ScorePointEducation>();
    public List<ScorePointGradeEducation> listGradeKind = new List<ScorePointGradeEducation>();
    protected List<SchoolInfo> listSchool = new List<SchoolInfo>();
    protected int examId = 0, classType = -1, subjectId = 0;
    private List<int> listTotal = new List<int>();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            listGradeKind = new List<ScorePointGradeEducation>();
            repeatEduKind.DataSource = GetSchoolName();
            repeatEduKind.DataBind();
        }
    }

    public List<SchoolInfo> GetSchoolName()
    {
        int examgrade = 0;
        string s = "";
        if (examId == 0 && EducationCookie.ExamID > 0)
        {
            examId = EducationCookie.ExamID;
        }
        ISchoolInfoLogic logic = Container<ISchoolInfoLogic>.Instance;
        listSchool = (List<SchoolInfo>)logic.SelectSchoolByExam(examId, CommonScore.GetAreaID);

        examgrade = EducationCookie.ExamGrade;

        foreach (SchoolInfo si in listSchool)
        {
            Hashtable ht = new Hashtable();
            ht.Add("Grade", Convert.ToInt32(examgrade));
            ht.Add("ExamID", examId);
            ht.Add("SchoolMark", si.SchoolMark);
            ht.Add("TableName", CommonScore.GetGradeStoreTable(examgrade));
            Reports.QueryExamCatch(1, Reports.CATCH_SCOREPOINTSET_GRADE).CheckCatch(ht);
        }

        return listSchool;
    }

    public string GetTH()
    {
        string s = "";
        if (examId == 0 && EducationCookie.ExamID > 0)
        {
            examId = EducationCookie.ExamID;
        }
        if (classType == -1 && EducationCookie.ExamClassType > -1)
        {
            classType = EducationCookie.ExamClassType;
        }


        string th = "<tr> <th colspan='2' rowspan='2' width=\"15%\" >学校</th> ";
        if (examId > 0 && classType != -1)
        {
            Hashtable table = new Hashtable();
            table.Add("ExamID", examId);
            table.Add("Type", classType);
            table.Add("SubjectID", subjectId);
            table.Add("Grade", EducationCookie.ExamGrade);
            IScorePointEducationLogic logic = Container<IScorePointEducationLogic>.Instance;
            List<ScorePointEducation> list = (List<ScorePointEducation>)logic.FindAll(table);
            listScoreSettings = new List<ScorePointEducation>();
            listScoreSettings = list;
            foreach (ScorePointEducation item in list)
            {
                string temp = "<th colspan='2' id='" + item.RID + "'>" + item.Name + "</th>";
                th += temp;
            }

            th += "<tr>";
            foreach (ScorePointEducation item in list)
            {
                string temp = "<th>命中</th><th>累计</th>";
                th += temp;

                listTotal.Add(0);
                listTotal.Add(0);
            }
            th += "</tr>";
        }
        th += "</tr>";
        return th;
    }

    /// <summary>
    /// 获取内容
    /// </summary>
    /// <returns></returns>
    public string GetTD(string schoolMark, string schoolName)
    {
        IScorePointGradeEducationLogic logic2 = Container<IScorePointGradeEducationLogic>.Instance;

        List<ScorePointGradeEducation> list2 = new List<ScorePointGradeEducation>();
        foreach (ScorePointEducation item in listScoreSettings)
        {
            Hashtable table = new Hashtable();
            table.Add("ItemID", item.RID);
            table.Add("SchoolMark", schoolMark);
            list2.AddRange((List<ScorePointGradeEducation>)logic2.FindAll(table));
        }

        List<ScorePointGradeEducation> listTemp = new List<ScorePointGradeEducation>();
        listTemp = list2;
        listGradeKind.AddRange(list2);

        string td = "<td colspan='2'  style='text-align:left;padding-left: 5px;'>" + schoolName + "</td>";

        foreach (ScorePointGradeEducation item2 in listTemp)
        {
            td += "<td  style='text-align:right;padding-right: 5px;'>" + item2.InNum + "</td><td  style='text-align:right;padding-right: 5px;'>" + item2.OverNum + "</td>";
        }
        return td;
    }

    /// <summary>
    /// 获取累计
    /// </summary>
    /// <returns></returns>
    public string GetAddUp()
    {
        string s = "<td colspan='2' rowspan='2'  style='text-align:left;padding-left: 5px;font-weight:bold;'>总计：</td> ";
        int totalInNum = 0, totalOverNum = 0, index = 0; ;

        if (listTotal.Count == listScoreSettings.Count * 2)
        {
            if (listGradeKind != null && listScoreSettings != null)
            {
                foreach (ScorePointEducation item in listScoreSettings)
                {
                    foreach (ScorePointGradeEducation item2 in listGradeKind)
                    {
                        if (item.RID == item2.ItemID)
                        {
                            listTotal[index * 2] += item2.InNum;
                            listTotal[index * 2 + 1] += item2.OverNum;
                        }
                    }
                    index++;
                }
            }
        }
        for (int i = 0; i < listScoreSettings.Count; i++)
        {
            totalInNum = listTotal[i * 2];
            totalOverNum = listTotal[i * 2 + 1];
            s += "<td  style='text-align:right;padding-right: 5px;font-weight:bold;'>" + totalInNum + "</td><td  style='text-align:right;padding-right: 5px;font-weight:bold;'>" + totalOverNum + "</td>";
        }
        return s;
    }


    protected void btnSelect_Click(object sender, EventArgs e)
    {

        string s = txtSubjectId.Text;
        if (!String.IsNullOrEmpty(s))
        {
            if (int.TryParse(s, out examId))
                subjectId = Convert.ToInt32(s);
        }

        examId = EducationCookie.ExamID;

        repeatEduKind.DataSource = GetSchoolName();
        repeatEduKind.DataBind();
    }
}
